package vectorstore

import (
	"math"
)

// cosineSimilarity 计算两个向量之间的余弦相似度
func cosineSimilarity(a, b []float64) float64 {
	if len(a) != len(b) {
		return 0
	}

	dotProduct := 0.0
	normA := 0.0
	normB := 0.0

	for i := 0; i < len(a); i++ {
		dotProduct += a[i] * b[i]
		normA += a[i] * a[i]
		normB += b[i] * b[i]
	}

	if normA == 0 || normB == 0 {
		return 0
	}

	return dotProduct / (math.Sqrt(normA) * math.Sqrt(normB))
}

// normalizeVector 归一化向量
func normalizeVector(v []float64) []float64 {
	var sum float64
	for _, val := range v {
		sum += val * val
	}

	norm := math.Sqrt(sum)
	if norm == 0 {
		return v
	}

	result := make([]float64, len(v))
	for i, val := range v {
		result[i] = val / norm
	}

	return result
}

// convertFloat32ToFloat64 将[]float32转换为[]float64
func convertFloat32ToFloat64(v []float32) []float64 {
	result := make([]float64, len(v))
	for i, val := range v {
		result[i] = float64(val)
	}
	return result
}

// convertFloat64ToFloat32 将[]float64转换为[]float32
func convertFloat64ToFloat32(v []float64) []float32 {
	result := make([]float32, len(v))
	for i, val := range v {
		result[i] = float32(val)
	}
	return result
}
